/**
 * 
 */
package com.xunchi.haichuan.data.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.struts2.ServletActionContext;

import com.netai.safedoc.utility.TransactionWrapper;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xunchi.haichuan.audit.service.LogService;
import com.xunchi.haichuan.audit.service.LogServiceImpl;
import com.xunchi.haichuan.configurator.model.SystemConfigs;
import com.xunchi.haichuan.data.model.Data;
import com.xunchi.haichuan.data.service.DataService;
import com.xunchi.haichuan.data.service.DataServiceImpl;
import com.xunchi.haichuan.user.model.User;

/**
 * 作者：kongxb<br>
 * 模块名称：数据管理模块<br>
 * 功能描述：数据列表<br>
 * 维护记录：2009.11.27, kongxb 创建该类
 */
public class DataListAction extends ActionSupport {

    private static final long serialVersionUID = -4516662197825782700L;

    private transient int start = 0;
    private transient int limit = 0;
    private transient boolean isSearch = false;
    private transient String province;
    private transient String city;
    private transient int validation;
    private transient Date beginDate;
    private transient Date endDate;
    private transient int count;

    private List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
    private int totalCount;

    private DataService dataService;
    private LogService logService;

    private User currentUser;

    public String execute() {
        HttpServletRequest request = ServletActionContext.getRequest();

        currentUser = (User) ActionContext.getContext().getSession().get("currentUser");
        String usrId = currentUser != null ? currentUser.getUsrId() : null;

        dataService = (DataService) TransactionWrapper.decorate(new DataServiceImpl());
        logService = (LogService) TransactionWrapper.decorate(new LogServiceImpl(), true);

        try {
            List<Data> list = new ArrayList<Data>();
            if (isSearch) { // 搜索数据
                if (currentUser.getPermissions().contains("searchData")) { // 有搜索权限
                    if (count == -1) {
                        list = dataService.search(province, city, validation, beginDate, endDate, start, limit);
                    } else {
                        list = dataService.search(province, city, validation, beginDate, endDate, count, start, limit);
                    }
                }
                if (SystemConfigs.isLog) {
                    logService.log("dataSearch", currentUser, 0);
                }
            } else {
                list = dataService.getLatestDatas(start, limit);
            }
            totalCount = dataService.getTotalCount();

            for (Iterator<Data> iter = list.iterator(); iter.hasNext();) {
                Data data = iter.next();

                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", data.getDataId());
                map.put("name", data.getName());
                map.put("personInCharge", data.getPersonInCharge());
                map.put("contact", data.getContact());
                map.put("tel", data.getTel());
                map.put("fax", data.getFax());
                map.put("mobile", data.getMobile());
                map.put("email", data.getEmail());
                map.put("website", data.getWebsite());
                map.put("address", data.getAddress());
                map.put("postcode", data.getPostcode());
                map.put("staffSize", data.getStaffSize());
                map.put("license", data.getLicense());
                map.put("registeredCapital", data.getRegisteredCapital());
                map.put("establishDate", data.getEstablishDate());
                map.put("industryType", data.getIndustryType());
                map.put("enterpriseNature", data.getEnterpriseNature());
                map.put("businessModel", data.getBusinessModel());
                map.put("businessArea", data.getBusinessArea());
                map.put("customerType", data.getCustomerType());
                map.put("telCode", data.getTelCode());
                map.put("province", data.getProvince());
                map.put("city", data.getCity());
                map.put("businessScope", data.getBusinessScope());
                map.put("brief", data.getBrief());
                map.put("legal", data.getLegal());
                map.put("customerSource", data.getCustomerSource());
                map.put("sourceUrl", data.getSourceUrl());
                map.put("country", data.getCountry());
                map.put("submitTime", DateFormatUtils.format(data.getSubmitTime(), "yyyy-M-d H:mm"));
                map.put("validation", data.getValidation());
                items.add(map);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return Action.SUCCESS;
    }

    /**
     * @param start
     *            the start to set
     */
    public void setStart(int start) {
        this.start = start;
    }

    /**
     * @param limit
     *            the limit to set
     */
    public void setLimit(int limit) {
        this.limit = limit;
    }

    /**
     * @param isSearch
     *            the isSearch to set
     */
    public void setIsSearch(boolean isSearch) {
        this.isSearch = isSearch;
    }

    /**
     * @param province
     *            the province to set
     */
    public void setProvince(String province) {
        this.province = province;
    }

    /**
     * @param city
     *            the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    public void setValidation(int validation) {
        this.validation = validation;
    }

    /**
     * @param beginDate
     *            the beginDate to set
     */
    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
    }

    /**
     * @param endDate
     *            the endDate to set
     */
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public void setCount(int count) {
        this.count = count;
    }

    /**
     * @return the items
     */
    public List<Map<String, Object>> getItems() {
        return items;
    }

    /**
     * @return the totalCount
     */
    public int getTotalCount() {
        return totalCount;
    }

}
